<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实时数据监控仪表盘</title>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
            color: #fff;
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1400px;
            margin: 0 auto;
        }
        
        header {
            text-align: center;
            padding: 20px 0;
            margin-bottom: 30px;
        }
        
        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
        }
        
        .description {
            font-size: 1.1rem;
            opacity: 0.9;
            max-width: 800px;
            margin: 0 auto;
        }
        
        .dashboard {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-bottom: 30px;
        }
        
        @media (max-width: 992px) {
            .dashboard {
                grid-template-columns: 1fr;
            }
        }
        
        .chart-container {
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.18);
        }
        
        .chart-title {
            font-size: 1.3rem;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
        }
        
        .chart-title i {
            margin-right: 10px;
            color: #4fc3f7;
        }
        
        .chart {
            height: 350px;
            width: 100%;
        }
        
        .controls {
            display: flex;
            justify-content: center;
            gap: 20px;
            margin-top: 20px;
        }
        
        .btn {
            padding: 12px 25px;
            border: none;
            border-radius: 50px;
            background: rgba(255, 255, 255, 0.15);
            color: white;
            font-size: 1rem;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
        }
        
        .btn:hover {
            background: rgba(255, 255, 255, 0.25);
            transform: translateY(-2px);
        }
        
        .btn:active {
            transform: translateY(0);
        }
        
        .btn i {
            font-size: 1.2rem;
        }
        
        .status-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 20px;
            padding: 15px 20px;
            background: rgba(0, 0, 0, 0.2);
            border-radius: 10px;
            font-size: 0.9rem;
        }
        
        .last-update {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .indicator {
            display: flex;
            align-items: center;
            gap: 6px;
        }
        
        .pulse {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background: #4CAF50;
            animation: pulse 1.5s infinite;
        }
        
        @keyframes pulse {
            0% {
                transform: scale(0.95);
                box-shadow: 0 0 0 0 rgba(76, 175, 80, 0.7);
            }
            
            70% {
                transform: scale(1);
                box-shadow: 0 0 0 10px rgba(76, 175, 80, 0);
            }
            
            100% {
                transform: scale(0.95);
                box-shadow: 0 0 0 0 rgba(76, 175, 80, 0);
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>实时数据监控仪表盘</h1>
            <p class="description">使用ECharts实现的动态时间轴，每隔2秒自动从模拟数据库获取数据并更新图表，展示实时数据变化趋势</p>
        </header>
        
        <div class="dashboard">
            <div class="chart-container">
                <div class="chart-title">
                    <i>📈</i> 销售趋势图
                </div>
                <div id="sales-chart" class="chart"></div>
            </div>
            
            <div class="chart-container">
                <div class="chart-title">
                    <i>👥</i> 用户访问量
                </div>
                <div id="users-chart" class="chart"></div>
            </div>
            
            <div class="chart-container">
                <div class="chart-title">
                    <i>💰</i> 交易金额统计
                </div>
                <div id="revenue-chart" class="chart"></div>
            </div>
            
            <div class="chart-container">
                <div class="chart-title">
                    <i>📊</i> 性能指标
                </div>
                <div id="performance-chart" class="chart"></div>
            </div>
        </div>
        
        <div class="controls">
            <button id="pause-btn" class="btn">
                <i>⏸️</i> 暂停更新
            </button>
            <button id="resume-btn" class="btn">
                <i>▶️</i> 继续更新
            </button>
            <button id="reset-btn" class="btn">
                <i>🔄</i> 重置数据
            </button>
        </div>
        
        <div class="status-bar">
            <div class="last-update">
                <div class="indicator">
                    <div class="pulse"></div>
                    <span>数据更新中</span>
                </div>
            </div>
            <div id="update-time">最后更新时间: --:--:--</div>
        </div>
    </div>

    <script>
        // 初始化图表
        const salesChart = echarts.init(document.getElementById('sales-chart'));
        const usersChart = echarts.init(document.getElementById('users-chart'));
        const revenueChart = echarts.init(document.getElementById('revenue-chart'));
        const performanceChart = echarts.init(document.getElementById('performance-chart'));
        
        // 当前时间数据
        let currentTime = new Date();
        let timeData = [currentTime.toTimeString().split(' ')[0]];
        
        // 初始化模拟数据
        let salesData = [Math.floor(Math.random() * 100) + 50];
        let usersData = [Math.floor(Math.random() * 200) + 100];
        let revenueData = [Math.floor(Math.random() * 1000) + 500];
        let performanceData = [Math.floor(Math.random() * 100) + 80];
        
        // 图表配置
        const commonOption = {
            grid: {
                top: 30,
                right: 30,
                bottom: 30,
                left: 50
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'line',
                    lineStyle: {
                        color: 'rgba(255, 255, 255, 0.2)'
                    }
                }
            },
            xAxis: {
                type: 'category',
                data: timeData,
                axisLine: {
                    lineStyle: {
                        color: 'rgba(255, 255, 255, 0.5)'
                    }
                }
            },
            yAxis: {
                type: 'value',
                axisLine: {
                    lineStyle: {
                        color: 'rgba(255, 255, 255, 0.5)'
                    }
                },
                splitLine: {
                    lineStyle: {
                        color: 'rgba(255, 255, 255, 0.1)'
                    }
                }
            },
            series: [{
                type: 'line',
                smooth: true,
                symbol: 'circle',
                symbolSize: 8,
                lineStyle: {
                    width: 3
                },
                areaStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: 'rgba(79, 195, 247, 0.6)' },
                        { offset: 1, color: 'rgba(79, 195, 247, 0.1)' }
                    ])
                }
            }]
        };
        
        // 设置各个图表的特定配置
        const salesOption = {
            ...commonOption,
            color: ['#4fc3f7'],
            series: [{
                ...commonOption.series[0],
                data: salesData,
                areaStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: 'rgba(79, 195, 247, 0.6)' },
                        { offset: 1, color: 'rgba(79, 195, 247, 0.1)' }
                    ])
                }
            }]
        };
        
        const usersOption = {
            ...commonOption,
            color: ['#ba68c8'],
            series: [{
                ...commonOption.series[0],
                data: usersData,
                areaStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: 'rgba(186, 104, 200, 0.6)' },
                        { offset: 1, color: 'rgba(186, 104, 200, 0.1)' }
                    ])
                }
            }]
        };
        
        const revenueOption = {
            ...commonOption,
            color: ['#4caf50'],
            series: [{
                ...commonOption.series[0],
                data: revenueData,
                areaStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: 'rgba(76, 175, 80, 0.6)' },
                        { offset: 1, color: 'rgba(76, 175, 80, 0.1)' }
                    ])
                }
            }]
        };
        
        const performanceOption = {
            ...commonOption,
            color: ['#ff9800'],
            series: [{
                ...commonOption.series[0],
                data: performanceData,
                areaStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: 'rgba(255, 152, 0, 0.6)' },
                        { offset: 1, color: 'rgba(255, 152, 0, 0.1)' }
                    ])
                }
            }]
        };
        
        // 渲染图表
        salesChart.setOption(salesOption);
        usersChart.setOption(usersOption);
        revenueChart.setOption(revenueOption);
        performanceChart.setOption(performanceOption);
        
        // 更新数据函数
        function updateData() {
            // 模拟从数据库获取新数据
            const now = new Date();
            const timeStr = now.toTimeString().split(' ')[0];
            
            // 添加新时间点
            timeData.push(timeStr);
            
            // 生成新数据（模拟数据库查询结果）
            salesData.push(Math.floor(Math.random() * 100) + 50);
            usersData.push(Math.floor(Math.random() * 200) + 100);
            revenueData.push(Math.floor(Math.random() * 1000) + 500);
            performanceData.push(Math.floor(Math.random() * 100) + 80);
            
            // 限制数据点数量，保持图表清晰
            if (timeData.length > 20) {
                timeData.shift();
                salesData.shift();
                usersData.shift();
                revenueData.shift();
                performanceData.shift();
            }
            
            // 更新图表
            salesChart.setOption({
                xAxis: {
                    data: timeData
                },
                series: [{
                    data: salesData
                }]
            });
            
            usersChart.setOption({
                xAxis: {
                    data: timeData
                },
                series: [{
                    data: usersData
                }]
            });
            
            revenueChart.setOption({
                xAxis: {
                    data: timeData
                },
                series: [{
                    data: revenueData
                }]
            });
            
            performanceChart.setOption({
                xAxis: {
                    data: timeData
                },
                series: [{
                    data: performanceData
                }]
            });
            
            // 更新最后刷新时间
            document.getElementById('update-time').textContent = 
                `最后更新时间: ${now.toLocaleTimeString()}`;
        }
        
        // 设置定时器，每2秒更新一次数据
        let updateInterval = setInterval(updateData, 2000);
        
        // 控制按钮事件处理
        document.getElementById('pause-btn').addEventListener('click', function() {
            clearInterval(updateInterval);
            document.querySelector('.pulse').style.background = '#f44336';
            document.querySelector('.indicator span').textContent = '更新已暂停';
        });
        
        document.getElementById('resume-btn').addEventListener('click', function() {
            clearInterval(updateInterval);
            updateInterval = setInterval(updateData, 2000);
            document.querySelector('.pulse').style.background = '#4CAF50';
            document.querySelector('.indicator span').textContent = '数据更新中';
        });
        
        document.getElementById('reset-btn').addEventListener('click', function() {
            // 重置数据
            timeData = [new Date().toTimeString().split(' ')[0]];
            salesData = [Math.floor(Math.random() * 100) + 50];
            usersData = [Math.floor(Math.random() * 200) + 100];
            revenueData = [Math.floor(Math.random() * 1000) + 500];
            performanceData = [Math.floor(Math.random() * 100) + 80];
            
            // 更新所有图表
            salesChart.setOption({
                xAxis: {
                    data: timeData
                },
                series: [{
                    data: salesData
                }]
            });
            
            usersChart.setOption({
                xAxis: {
                    data: timeData
                },
                series: [{
                    data: usersData
                }]
            });
            
            revenueChart.setOption({
                xAxis: {
                    data: timeData
                },
                series: [{
                    data: revenueData
                }]
            });
            
            performanceChart.setOption({
                xAxis: {
                    data: timeData
                },
                series: [{
                    data: performanceData
                }]
            });
        });
        
        // 窗口大小调整时重置图表大小
        window.addEventListener('resize', function() {
            salesChart.resize();
            usersChart.resize();
            revenueChart.resize();
            performanceChart.resize();
        });
    </script>
</body>
</html>